# _*_coding=utf-8_*_
import time
from framework.logger import MyLog
from selenium.common.exceptions import NoSuchElementException
import os

# 实例化日志
log = MyLog.get_log()
logger = log.getlog()


class BasePage(object):

    def __init__(self, driver):
        self.driver = driver

    # 保存图片
    def get_screen_shot(self):
        """
        把file_path这个参数写死，直接保存到项目根目录的一个文件夹.\Screenshots下
        :return:
        """
        project_path = os.path.dirname(os.path.abspath('.'))
        file_path = project_path + r'/screenshots/'
        rq = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
        screen_name = file_path + rq + '.png'
        try:
            self.driver.get_screenshot_as_file(screen_name)
            logger.info('已采取截图并保存到文件夹：/screenshots' + screen_name)
        except Exception as e:
            logger.error('未能采取截图！%s' % e)
            self.get_screen_shot()

    # 重写定位方法
    def find_element(self, selector):
        """
        to locate element by selector
        :arg
        selector should be passed by an example with "i,xxx"
        "x,//*[@id='langs']/button"
        :returns
        DOM element
        """
        if ',' not in selector:
            return self.driver.find_element_by_id(selector)
        selector_by = selector.split(',')[0]
        selector_value = selector.split(',')[1]

        if selector_by == "i" or selector_by == 'id':
            element = self.driver.find_element_by_id(selector_value)
        elif selector_by == "n" or selector_by == 'name':
            element = self.driver.find_element_by_name(selector_value)
        elif selector_by == "c" or selector_by == 'class_name':
            element = self.driver.find_element_by_class_name(selector_value)
        elif selector_by == "l" or selector_by == 'link_text':
            element = self.driver.find_element_by_link_text(selector_value)
        elif selector_by == "p" or selector_by == 'partial_link_text':
            element = self.driver.find_element_by_partial_link_text(selector_value)
        elif selector_by == "t" or selector_by == 'tag_name':
            element = self.driver.find_element_by_tag_name(selector_value)
        elif selector_by == "x" or selector_by == 'xpath':
            element = self.driver.find_element_by_xpath(selector_value)
        elif selector_by == "s" or selector_by == 'selector_selector':
            element = self.driver.find_element_by_css_selector(selector_value)
            
        else:
            raise NameError("Please enter a valid type of targeting elements.")

        return element

    def send_str(self, selector, text):
        """
        Operation input box.

        Usage:
        driver.type("i,el","selenium")
        """
        el = self.find_element(selector)
        el.clear()
        el.send_keys(text)

    def click(self, selector):
        el = self.find_element(selector)
        try:
            el.click()
            logger.info("元素 \' %s \' 已点击。" % el.text)
        except NoSuchElementException as e:
            logger.error("没有点击的元素: %s" % e)

    # 检查元素是否存在
    def check_element(self, selector):
        try:
            self.find_element(selector)
            pass
        except NoSuchElementException as e:
            logger.error('NoSuchElementException: %s' % e)
            self.get_screen_shot()
